// myself/pages/medal-detail/medalDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    CDNPATH:app.globalData.CDNPATH,
    medalList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMedalList()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getUserMedalInfo()
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  getMedalList () {
    app.api.post({
      url: 'qry_medal_list'
    }).then(res=>{
      this.setData({
        medalList:res || []
      })
    })
  },
  getUserMedalInfo () {
    let userInfo = { ...app.globalData.userInfo }
    app.api.get({
      url: 'cpic_user_getUserInfoForVX'
    }).then(res => {
      userInfo.realName = res.realName
      userInfo.medalNum = res.medalNum || 0
      this.setData({ info: userInfo })
    })
  },
  // 领取勋章
  medalClick (e) {
    console.log(e,'e============');
    const id = e.currentTarget.dataset.id
    const state = e.currentTarget.dataset.state
    if(state !== '待领取'){
      return false
    }
    app.api.get({
      url: 'user_receive_medal',
      data:{id:id}
    }).then(res=>{
      this.getUserMedalInfo()
      this.getMedalList()
      wx.showModal({
        content:'领取成功',
        showCancel:false
      })
    })
  }
})